import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSentineled;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2023-12-14
 * Time: 16:18
 */
public class RedisString {

    public static void test1(Jedis jedis) {
        System.out.println("mset/mget");
        jedis.flushAll();

        jedis.mset("key1","111","key2","222","key3","333");
        // 获取到的value顺序和key是匹配的，如果某个可以的value不存在，使用null表示
        List<String> mget = jedis.mget("key1", "key2", "key100", "key3");
        System.out.println(mget);
    }

    public static void test2(Jedis jedis) {
        System.out.println("getrange/setrange");
        jedis.flushAll();

        jedis.set("key1","abcdefg");
        String result1 = jedis.getrange("key1", 2, 4);
        System.out.println("result1 = "+result1);

        jedis.setrange("key1",2,"aaaaaaaa");
        String result2 = jedis.get("key1");
        System.out.println("result2 = "+result2);

    }

    public static void test3(Jedis jedis) {
        System.out.println("append/decr/incr");
        jedis.flushAll();

        jedis.set("key2","abcd");
        String value2 = jedis.get("key2");
        System.out.println("value2 = "+value2);
        jedis.append("key2","efg");
        String value3 = jedis.get("key2");
        System.out.println("value3 = "+value3);

        jedis.set("key1","100");
        String value1 = jedis.get("key1");
        System.out.println("value1 = " + value1);
        jedis.incr("key1");
        value2 = jedis.get("key1");
        System.out.println("value2 = "+value2);
        jedis.decr("key1");
        value3 = jedis.get("key1");
        System.out.println("value3 = " + value3);
    }

    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:6379");
        try (Jedis jedis = jedisPool.getResource()) {
//            test1(jedis);
//            test2(jedis);
            test3(jedis);
        }

    }
}
